﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using T.FAS.Runtime.Metadata.Entity;
using T.ServiceComponent.DAC.Service;
using T.ServiceComponent.DataAccessEx.Core;

namespace T.FAS.Runtime.Metadata.Dao
{
    public class MetaDataCheckOutContentDao : DaoService<MetaDataCheckOutContent>
    {
        public MetaDataCheckOutContentDao(string db = "TFAS") : base()
        {
            this.DBName = db;
        }

        public override string IDColumnName
        {
            get
            {
                return "ID";
            }
        }

        public override string TableName
        {
            get
            {
                return "Meta_CheckOutContent";
            }
        }

        public void UpdateMetaDataContent(string metaDataContent, string checkOutID)
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            paramsList.Add(this.DB.MakeInParam("@MetaDataContent", TDbDataType.VarChar, metaDataContent));
            paramsList.Add(this.DB.MakeInParam("@CheckOutID", TDbDataType.VarChar, checkOutID));
            this.DB.ExecSqlStatement("update Meta_CheckOutContent set MetaDataContent=@MetaDataContent where CheckOutID=@CheckOutID", paramsList.ToArray());
        }

        public string GetContent(string checkOutID)
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            paramsList.Add(this.DB.MakeInParam("@CheckOutID", TDbDataType.VarChar, checkOutID));
            var ds = this.DB.ExecuteDataSet("select MetaDataContent FROM Meta_CheckOutContent where CheckOutID=@CheckOutID", paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) {
                return Convert.ToString(ds.Tables[0].Rows[0]["MetaDataContent"]);
            }
            return "";
        }

        public void UpdateCheckOutContent(string metaDataID, string projectBranchID, string userID, string metaDataContent)
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            paramsList.Add(this.DB.MakeInParam("@MetaDataContent", TDbDataType.VarChar, metaDataContent));
            paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metaDataID));
            paramsList.Add(this.DB.MakeInParam("@CheckOutUserID", TDbDataType.VarChar, userID));
            paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchID));

            this.DB.ExecSqlStatement("Update Meta_CheckOutContent Set MetaDataContent=@MetaDataContent WHERE CheckOutID in (Select ID from Meta_CheckOut where MetaDataID=@MetaDataID AND CheckOutUserID=@CheckOutUserID AND ProjectBranchID=@ProjectBranchID AND CheckOutState!=-1)", paramsList.ToArray());
        }
    }
}
